394. 字符串解码
为保证权益,题目请参考 394. 字符串解码(From LeetCode).
解决方案1
Python
python
# 394. 字符串解码
# https://leetcode-cn.com/problems/decode-string/
class Solution:
def decodeString(self, s: str) -> str:
stacks = []
for st in s:
if st == "]":
tmpStr = ""
while True:
t = stacks.pop()
if t == "[":
break
else:
tmpStr = t + tmpStr
num = 0
i = 0
while True:
if len(stacks) == 0:
break
t = stacks[-1]
if t.isdigit():
stacks.pop()
num += int(t) * (10 ** i)
i += 1
else:
break
stacks.append(tmpStr * num)
else:
stacks.append(st)
ans = "".join(stacks)
return ans
if __name__ == "__main__":
s = Solution()
print(s.decodeString("3[a]2[b]"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42